﻿using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DAL
{
    public class WhStockTakeDetailDal
    {
        private readonly AppDbContext _dbContext;

        public WhStockTakeDetailDal(AppDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="data">实体</param>
        /// <returns>结果</returns>
        public async Task<int> CreateAsync(WhStockTakeDetail data)
        {
            _dbContext.WhStockTakeDetail.Add(data);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="Id">ID</param>
        /// <returns>盘点明细</returns>
        public async Task<WhStockTakeDetail> GetByIdAsync(long Id)
        {
            return await _dbContext.WhStockTakeDetail.FirstOrDefaultAsync(g => g.InventoryOrderDetailId == Id);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns>盘点明细列表</returns>
        public async Task<List<WhStockTakeDetail>> GetAllAsync()
        {
            return await _dbContext.WhStockTakeDetail.ToListAsync();
        }

        /// <summary>
        /// 根据盘点单ID查询明细
        /// </summary>
        /// <param name="inventoryOrderId">盘点单ID</param>
        /// <returns>盘点明细列表</returns>
        public async Task<List<WhStockTakeDetail>> GetByInventoryOrderIdAsync(long inventoryOrderId)
        {
            return await _dbContext.WhStockTakeDetail
                .Where(d => d.InventoryOrderId == inventoryOrderId)
                .ToListAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="data">包含更新信息</param>
        public async Task<int> UpdateAsync(WhStockTakeDetail data)
        {
            _dbContext.WhStockTakeDetail.Update(data);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="Id">ID</param>
        public async Task<int> DeleteAsync(long Id)
        {
            var data = await GetByIdAsync(Id);
            if (data != null)
            {
                _dbContext.WhStockTakeDetail.Remove(data);
                return await _dbContext.SaveChangesAsync();
            }
            return 0;
        }

         /// <summary>
        /// 根据多个盘点单ID查询明细
        /// </summary>
        /// <param name="takeIds">盘点单ID列表</param>
        /// <returns>明细列表</returns>
        public async Task<List<WhStockTakeDetail>> GetByTakeIdsAsync(List<long> takeIds)
        {
            // 验证参数
            if (takeIds == null || !takeIds.Any())
            {
                return new List<WhStockTakeDetail>();
            }

            // 查询指定盘点单ID对应的明细（排除已删除的记录）
            return await _dbContext.WhStockTakeDetail
                .Where(d => takeIds.Contains(d.InventoryOrderId) && d.IsDelete != "Y")
                .ToListAsync();
        }
    }
}